package k3_IO.p2_fileStream.demo01;

import java.io.*;

public class BufferedStream {

    public static void bufferedOutputStream() throws Exception{

        FileOutputStream fos = new FileOutputStream("E://a.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        bos.write(98);
        bos.write("hello".getBytes());
        bos.write("\n".getBytes());

        bos.flush();
        bos.close();

    }

    public static void bufferedInputStream() throws Exception{

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E://a.txt"));

        byte[] allBytes = bis.readAllBytes();

        System.out.println(new String(allBytes));

        bis.close();

    }

    public static void bufferedWriter() throws Exception{

        BufferedWriter bw = new BufferedWriter(new FileWriter("E://b.txt"));

        bw.write("hello");
        //对于字符缓冲输出流来说，多提供了一个换行的方法
        bw.newLine();
        bw.write("world");

        bw.close();

    }

    public static void bufferedReader() throws Exception{

        BufferedReader br = new BufferedReader(new FileReader("E://b.txt"));

        String line = "";
        //readLine是缓冲字符输入流提供的可以一次读取一行内容的方法
        while((line = br.readLine())!=null){
            System.out.println(line);
        }

        br.close();
    }

    public static void copyFileByStream() throws Exception{

        FileInputStream fis = new FileInputStream("E://a.exe");
        FileOutputStream fos = new FileOutputStream("E://b.exe");

        int b = 0;
        while((b = fis.read())!=-1){
            fos.write(b);
        }

        fos.close();
        fis.close();

    }

    public static void copyFileByBufferedStream() throws Exception{

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E://a.exe"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E://b.exe"));

        int b = 0;
        while((b = bis.read())!=-1){
            bos.write(b);
        }

        bos.close();
        bis.close();

    }

    public static void copyFileByBufferedStreamBytes() throws Exception{

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E://a.exe"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E://b.exe"));

        byte[] byteBuffer = new byte[8*1024];
        int length = 0;
        while((length = bis.read(byteBuffer))!=-1){
            bos.write(byteBuffer,0,length);
        }

        bos.close();
        bis.close();

    }

    public static void main(String[] args) throws Exception {

//        BufferedStream.bufferedOutputStream();
//        BufferedStream.bufferedInputStream();
//        BufferedStream.bufferedWriter();
//        BufferedStream.bufferedReader();

        long startTime = System.currentTimeMillis();
//        BufferedStream.copyFileByStream();
//        BufferedStream.copyFileByBufferedStream();
        copyFileByBufferedStreamBytes();
        long endTime = System.currentTimeMillis();

        System.out.println(endTime-startTime);


    }

}
